﻿/******************************************************************************
 * Name			: Packet.cpp
 * Author		: 671643387
 * Date			: 2015年9月13日
 * Description	:
 *****************************************************************************/

#include "Packet.hpp"
using namespace sxr::common;

Packet::Packet(int id, size_t size, char* buff)
	: id_(id)
	, size_(size)
	, buffer_(buff)
	, bytsWrote_(0)
{

}

Packet::~Packet()
{

}

bool Packet::canReadPacket(dev::net::TcpConnectionInputStream& is, struct _stHead& head)
{
    if (is.size() > sizeof(head))
    {
        is.peek((char*)&head, sizeof(head));
        return is.size() >= head.size;
    }
    return false;
}

bool Packet::read(dev::net::TcpConnectionInputStream& is)
{
	return (is.read(buffer_, size_) == size_);
}

void Packet::write(dev::net::TcpConnectionOutputStream& os)
{
    bytsWrote_ += os.write(buffer_ + bytsWrote_, size_ - bytsWrote_);
}